package br.project.cdd.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import br.project.cdd.bean.UserBean;

public class UserDAO extends Conexao {
	
	public UserDAO(){
		super();
	}
	
	public void inserirUsuario(UserBean bean, int perfil){
		
		try{
			String sql = "INSERT INTO USUARIO (NM_USUARIO, SENHA, ID_PERFIL) VALUES ('" + bean.getNmUsuario() + "', '" + bean.getSenha() + "'," + perfil + ")";
			Statement stmt = conexao.createStatement();
			stmt.executeUpdate(sql);
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
	}
	
	public UserBean buscarUsuario(UserBean user){
		UserBean userBean = null;
		
		try{
			String sql = "SELECT ID_USUARIO, NM_USUARIO, ID_PERFIL FROM USUARIO WHERE SENHA='" + user.getSenha() + "' AND NM_USUARIO='" + user.getNmUsuario() + "';";
			Statement stmt = conexao.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			
			while(rs.next()){
				userBean = new UserBean();
				
				userBean.setIdUsuario(rs.getInt(1));
				userBean.setNmUsuario(rs.getString(2));
				userBean.setIdPerfil(3);
			}
		}
		catch(SQLException sql){
			sql.printStackTrace();
		}
		
		return userBean;
	}
}
